package com.example.larouzey.fragmenttest201ljy.model;


import com.example.larouzey.fragmenttest201ljy.bean.ArticleBean;
import com.example.larouzey.fragmenttest201ljy.iface.ArticleListener;
import com.example.larouzey.fragmenttest201ljy.iface.Articleiface;
import com.example.larouzey.fragmenttest201ljy.service.ArticleService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Larouzey on 2017/4/19.
 */

public class ArticleModel implements Articleiface {
    public static Retrofit retrofit;
    public static final String BASEURL = "http://amicool.neusoft.edu.cn/";

    public ArticleModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public void getResultList(String mod, int page, String sessionID, final ArticleListener listener) {
        ArticleService articleService = retrofit.create(ArticleService.class);
        Call<List<ArticleBean>> call = articleService.getResult(mod, page, sessionID);
        call.enqueue(new Callback<List<ArticleBean>>() {
            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else
                    listener.onFail("on response fail");
            }

            public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }


}
